package com.zhiyou.service.SystemUserService.Impl;

import com.zhiyou.bean.User;
import com.zhiyou.dao.UserMapper;
import com.zhiyou.service.SystemUserService.SystemUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

@Service
public class SystemUserServiceImpl implements SystemUserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public ArrayList<User> selectAllUser() {
        ArrayList<User> userlist =  userMapper.selectUserAll();
        return userlist;
    }

    @Override
    public ArrayList<User> selectByName(String username) {
        ArrayList<User> user  = userMapper.selectByUserName(username);
        return user;
    }

    @Override
    public int userInsert(User user,int rid) {

        int f1= userMapper.insert(user);

        User user1 = userMapper.selectOne(user);
        System.out.println(user1.getuId());

        int f2= userMapper.insertUidRid(user1.getuId(),rid);

        return f2;
    }

    @Override
    public int updateUser(User user, int rid) {
        System.out.println(user.toString());
        System.out.println(rid);
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("uId",user.getuId());
        int i = userMapper.updateByExample(user, example);
        System.out.println(i+"user信息改了。角色没改");
        int i1 = userMapper.updateRoleByUid(user.getuId(),rid);
        System.out.println("角色信息也改了");
        return i1;
    }

    @Override
    public int delUserByid(User user) {
        //假删除
        int i = userMapper.deleteByUid(user.getuId());
//        user.setuStatus(0);
//        int i = userMapper.updateByPrimaryKeySelective(user);
        System.out.println(i);
        return i;
    }

    @Override
    public User selectEditInfo(User user) {
        User user1 = userMapper.selectOne(user);
        System.out.println(user1.getuUsername());
        return user1;
    }
}
